package in.iqing.view.activity;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.lidroid.xutils.exception.DbException;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.OnClick;
import in.iqing.app.R;
import in.iqing.base.BaseActivity;
import in.iqing.control.adapter.DirectoryAdapter;
import in.iqing.control.helper.IntentHelper;
import in.iqing.control.util.IntentUtil;
import in.iqing.model.bean.Book;
import in.iqing.model.bean.Chapter;
import in.iqing.model.bean.DirectoryItem;
import in.iqing.model.bean.History;
import in.iqing.model.bean.Volume;
import in.iqing.model.db.DBHelper;
import in.iqing.view.widget.SimpleDividerItemDecoration;

/**
 * Created by ZuoShu on 2015/7/17.
 */
public class CachedDirectoryActivity extends BaseActivity {
    private Book book;

    @Bind(R.id.title)
    TextView title;

    @Bind(R.id.directory_recycler)
    RecyclerView directoryRecyclerView;

    private DirectoryAdapter directoryAdapter;

    private List<Chapter> chapters;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_cached_directory);
    }

    @Override
    protected void onInitialize(Bundle savedInstanceState) {
        super.onInitialize(savedInstanceState);
        book = (Book) getIntent().getSerializableExtra("book");
        title.setText(book.getTitle());
        setRecyclerView();
        loadDirectory();
    }

    private void setRecyclerView() {
        directoryAdapter = new DirectoryAdapter();
        directoryRecyclerView.setAdapter(directoryAdapter);
        directoryRecyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        directoryRecyclerView.addItemDecoration(new SimpleDividerItemDecoration(getContext()));
        directoryAdapter.setListener(new ExtChapterClickListener());
        History bookmark = DBHelper.getInstance().getBookmark(book.getId());
        if (bookmark != null) {
            directoryAdapter.setLastReadChapterId(bookmark.getChapterId());
        }
    }

    private void loadDirectory() {
        List<Volume> volumes = DBHelper.getInstance().getBookVolumes(book);
        if (volumes == null || volumes.size() == 0) {
            showEmpty();
            return;
        }

        List<DirectoryItem> directoryItems = new ArrayList<>();
        chapters = new ArrayList<>();
        for (Volume volume : volumes) {
            directoryItems.add(DirectoryItem.fromVolume(volume));
            if (volume.getChapters() != null && volume.getChapters().size() > 0) {
                for (Chapter chapter : volume.getChapters()) {
                    directoryItems.add(DirectoryItem.fromChapter(chapter));
                    chapters.add(chapter);
                }
            }
        }

        directoryAdapter.setDirectoryItems(directoryItems);
        directoryAdapter.addDownloadedChapters(DBHelper.getInstance().getDownloadedChapters(book));
        directoryAdapter.notifyDataSetChanged();
        showContent();
    }

    @OnClick(R.id.add_menu)
    public void onAddClick(View v) {
        Bundle data = new Bundle();
        data.putSerializable("book", book);
        IntentHelper.startActivity(this, DirectoryActivity.class, data);
    }

    private class ExtChapterClickListener implements DirectoryAdapter.OnChapterClickListener {

        @Override
        public void onChapterClick(DirectoryItem chapter) {
            Chapter qingChapter = chapter.getChapter();

            Bundle extra = getIntent().getExtras();
            extra.putSerializable("chapter", qingChapter);
            extra.putSerializable("chapters", (Serializable) chapters);
            extra.putSerializable("book", book);
            extra.putBoolean("from_chapter", true);
            IntentUtil.startActivity(getActivity(), ContentActivity.class, extra);
        }
    }
}
